﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Threading;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace TunaInterpreter2
{
    /// <summary>
    /// Interaction logic for SplashScreen.xaml
    /// </summary>
    public partial class SplashScreen : Window
    {
        int count = 0;
        int countMax = 3;
        DispatcherTimer timer;
        Random random;

        public SplashScreen()
        {
            InitializeComponent();
        }

        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            random = new Random();

            Thread thread = new Thread(() => fakeLoading());
            thread.Name = "LOADING THREAD";
            Console.WriteLine("#### STARTING LOADING THREAD");
            thread.Start();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(100);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (progressBar.Value >= 100)
            {
                timer.Stop();
                this.Close();
            }
        }

        void setLoaderText(String s)
        {
            txtLoader.Content = "Loading: " + s;
        }

        void fakeLoading()
        {
            Random randomLength = new Random();

            for(int i=0 ; i<=100 ; i++)
            {
                this.Dispatcher.BeginInvoke(
                    (Action)delegate()
                    {
                        setLoaderText("Tuna Engine\\" + RandomString(randomLength.Next(5, 35)) + ".dll");
                        progressBar.Value = i;
                        txtProgress.Content = i + "%";
                    });

                System.Threading.Thread.Sleep(100);
            }
        }
    }
}
